[์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step - ๋ฐ์ฌ์ฑ]์ ์ฝ๊ณ ์ธ์ฉํ๊ณ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
3์ฅ์ ๋ก์ปฌ์์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐ, ๋ฒ์ ๊ด๋ฆฌ ์์คํ ์์ ์์ค์ฝ๋๋ฅผ ๊ด๋ฆฌ, ๊ฐ๋ฐํ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์๊ฒฉ ์๋ฒ์ ์ค์ ๋ฐฐํฌํ๋ ๊ฒฝํ์ ํ๊ณ , ๊ตฌํํ ์ง๋ฌธ/๋ต๋ณ ๊ฒ์ํ์ ๋ํ ์๋น์ค ์๊ตฌ์ฌํญ๊ณผ ์ค์ต ์๊ตฌ์ฌํญ์ ์ ์ํ๋ค. ๋ํ, HTTP ์น ์๋ฒ๋ฅผ ์ง์ ๊ตฌํํ๋ ๊ฒฝํ์ ํจ์ผ๋ก์จ ์น ํด๋ผ์ด์ธํธ์ ์๋ฒ ๊ฐ ๋ฐ์ดํฐ๋ฅผ ์ด๋ป๊ฒ ์ฃผ๊ณ ๋ฐ๋์ง์ ๋ํด ํ์ตํ๋ค.
๐ฉ ์๋น์ค ์๊ตฌ์ฌํญ
์ง๋ฌธ/๋ต๋ณ ๊ฒ์ํ
๐ง ์ง๋ฌธ ๋ชฉ๋ก ํ๋ฉด
- ํ์๊ฐ์
- ๋ก๊ทธ์ธ
- ๋ก๊ทธ์์
- ๊ฐ์ธ์ ๋ณด ์์
- ์ง๋ฌธํ๊ธฐ
-
๊ฐ ์ง๋ฌธ ์ ๋ชฉ์ ํด๋ฆญํ์ฌ ์์ธ๋ณด๊ธฐ ํ๋ฉด์ผ๋ก ์ด๋
- ๋ต๋ณ ์ถ๊ฐ, ์ง๋ฌธ๊ณผ ๋ต๋ณ์ ์์ /์ญ์
๐ฉ ๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถ
์๋ฐ 8๋ฒ์ & Intellij ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ
https://github.com/slipp/web-application-server, https://www.youtube.com/watch?v=5hjYe_PggJI ์ฐธ๊ณ !
๐ฉ ์๊ฒฉ ์๋ฒ์ ๋ฐฐํฌ
์ ์์ผ ํ๋ก์ธ์ค์ ์ ๊ทผ ๋ฐฉ์ : ํ ์์ ์ ๊ฐ์ฅ ๊ฐ์น์๋, ๋์ํ๋ ์ํํธ์จ์ด๋ฅผ ๋ง๋๋ ๊ฒ์ ์์น์ผ๋ก ํ๋ ๋ฐฉ์์ด๋ค.
๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ์ ๊ตฌ์ถํ ํ ๋ฐ๋ก ์ค์ต ๋จ๊ณ๋ฅผ ์งํํ ์ ์์ง๋ง ์ค์ต์ ์งํํ๊ธฐ ์ ์ ๋จผ์ HTTP ์น ์๋ฒ๋ฅผ ์๊ฒฉ ์๋ฒ์ ๋ฐฐํฌํด๋ณด์. ์ด๋ฌํ ๊ฒฝํ์ ํตํด ํฐ๋ฏธ๋ ํ๊ฒฝ์์ ์์ ํ๋ ๊ฒ์ ์ต์ํด์ง๋๋ก ํ๋ค. https://opentutorials.org/module/1946 ์ฐธ๊ณ ํ์ฌ AWS์ ๋ํ ํ์๊ฐ์ , ์ฐ๋ถํฌ ์ด์์ฒด์ ์ค์น, SSH๋ฅผ ํตํ ์ ๊ทผ์ ์งํํ ์ ์๋ค.
๐ง ์๊ตฌ์ฌํญ
๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ์ ์ค์นํ HTTP ์น ์๋ฒ๋ฅผ ๋ฌผ๋ฆฌ์ ์ผ๋ก ๋จ์ด์ ธ ์๋ ์๊ฒฉ ์๋ฒ์ ๋ฐฐํฌํด ์ ์์ ์ผ๋ก ๋์ํ๋์ง ํ ์คํธํ๋ค.
๐ฉ ์น ์๋ฒ ์ค์ต
๐ง ์ค์ต ํ๊ฒฝ ์ธํ ๋ฐ ์์ค์ฝ๋ ๋ถ์
์ค์ต์ผ๋ก ์งํํ HTTP ์น ์๋ฒ๋ ์์ ๋ก์ปฌ ๊ฐ๋ฐ ํ๊ฒฝ์์ ์ธํ ํ Git ์ ์ฅ์์ master ๋ธ๋์น์์ ์์ํ๋ฉด ๋๋ค.
์ค์ต HTTP ์น ์๋ฒ์ ํต์ฌ์ด ๋๋ ์ฝ๋๋ webserver ํจํค์ง์ WebServer, RequestHandler ํด๋์ค์ด๋ค.
WebServerํด๋์ค๋ ์น ์๋ฒ๋ฅผ ์์ํ๊ณ , ์ฌ์ฉ์์ ์์ฒญ์ด ์์ ๋๊น์ง ๋๊ธฐ ์ํ์ ์๋ค๊ฐ ์ฌ์ฉ์ ์์ฒญ์ด ์์ ๊ฒฝ์ฐ ํด๋น ์์ฒญ์RequestHandlerํด๋์ค์ ์์ํ๋ ์ญํ ์ ์ํํ๋ค.
// in WebServer.java
try (ServerSocket listenSocket = new ServerSocket(port)) {
log.info("Web Application Server started {} port.", port);
// ํด๋ผ์ด์ธํธ๊ฐ ์ฐ๊ฒฐ๋ ๋๊น์ง ๋๊ธฐํ๋ค.
Socket connection;
while ((connection = listenSocket.accept()) != null) {
RequestHandler requestHandler = new RequestHandler(connection);
requestHandler.start();
}
}
- ์ฌ์ฉ์ ์์ฒญ์ด ๋ฐ์ํ ๋๊น์ง ๋๊ธฐ ์ํ์ ์๋๋ก ์ง์ํ๋ ์ญํ ์ ์๋ฐ์ ํฌํจ๋์ด ์๋
ServerSocketํด๋์ค๊ฐ ๋ด๋นํ๋ค. - ์ฌ์ฉ์ ์์ฒญ์ด ๋ฐ์ํ๋ ์๊ฐ ํด๋ผ์ด์ธํธ์ ์ฐ๊ฒฐ์ ๋ด๋นํ๋
Socket์RequestHandler์ ์ ๋ฌํ๋ฉด์ ์๋ก์ด ์ค๋ ๋๋ฅผ ์คํํ๋ ๋ฐฉ์์ผ๋ก ๋ฉํฐ์ค๋ ๋ ํ๋ก๊ทธ๋๋ฐ์ ์ง์ํ๋ค.
// in RequestHandler.java
package webserver;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RequestHandler extends Thread {
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class);
private Socket connection;
public RequestHandler(Socket connectionSocket) {
this.connection = connectionSocket;
}
public void run() {
log.debug("New Client Connect! Connected IP : {}, Port : {}", connection.getInetAddress(),
connection.getPort());
try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) {
// TODO ์ฌ์ฉ์ ์์ฒญ์ ๋ํ ์ฒ๋ฆฌ๋ ์ด ๊ณณ์ ๊ตฌํํ๋ฉด ๋๋ค.
DataOutputStream dos = new DataOutputStream(out);
byte[] body = "Hello World".getBytes();
response200Header(dos, body.length);
responseBody(dos, body);
} catch (IOException e) {
log.error(e.getMessage());
}
}
private void response200Header(DataOutputStream dos, int lengthOfBodyContent) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes("Content-Type: text/html;charset=utf-8\r\n");
dos.writeBytes("Content-Length: " + lengthOfBodyContent + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
}
}
private void responseBody(DataOutputStream dos, byte[] body) {
try {
dos.write(body, 0, body.length);
dos.flush();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
RequestHandlerํด๋์ค๋Thread๋ฅผ ์์ํ๊ณ , ์ฌ์ฉ์์ ์์ฒญ์ ๋ํ ์ฒ๋ฆฌ์ ์๋ต์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ๋ด๋นํ๋ ํด๋์ค์ด๋ค.-
์์ผ๋ก์ ๋ชจ๋ ์ค์ต์
RequestHandlerํด๋์ค์run()๋ฉ์๋์์ ๊ตฌํํ ์ ์๋ค.- ํด๋น ๋ฉ์๋์ ๋ณต์ก๋๊ฐ ์ฆ๊ฐํ๋ ๊ฒฝ์ฐ, ์๋ก์ด ํด๋์ค ๋๋ ๋ฉ์๋๋ก ๋ถ๋ฆฌํ๋ ๋ฐฉ์์ผ๋ก ๋ฆฌํฉํ ๋ง์ ์ํํ ์ ์๋ค.
InputStream: ํด๋ผ์ด์ธํธ(์น ๋ธ๋ผ์ฐ์ )์์ ์๋ฒ๋ก ์์ฒญ์ ๋ณด๋ผ ๋ ์ ๋ฌ๋๋ ๋ฐ์ดํฐ๋ฅผ ๋ด๋น-
OutputStream: ์๋ฒ์์ ํด๋ผ์ด์ธํธ์ ์๋ต์ ๋ณด๋ผ ๋ ์ ๋ฌ๋๋ ๋ฐ์ดํฐ๋ฅผ ๋ด๋น- ์น ์๋ฒ ์ ์ฅ์์ ์ดํด!
์ ์ฒด์ ์ธ ํ๋ฆ์,
๋ธ๋ผ์ฐ์ ์WebServer์ RequestHandler๋Socket์ด๋ผ๋ ํต๋ก๋ก ํต์ ํ๋ค.- ํด๋น ํต๋ก์
InputStream&OutputStream์๋จ์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋๋ค.
"ํ๋ก๊ทธ๋๋ฐ ์คํ ์ค ๋ฐ์ํ๋ ๋ก๊ทธ ๋ฉ์ธ์ง๋ฅผ ์ฃผ์ ๊น๊ฒ ์ดํด๋ณด๋ ๊ฒ์ ์ข์ ์ต๊ด ์ค ํ๋์ด๋ค."
๐ง ์ค์ต ์๊ตฌ์ฌํญ
์๊ตฌ์ฌํญ 1 - index.html ์๋ตํ๊ธฐ
: ํ์ฌ HTTP ์น ์๋ฒ์ ์ ์ํ๋ฉด ๋ฌด์กฐ๊ฑด "Hello World" ๋ผ๋ ๋ฌธ๊ตฌ๋ง ๋ฌ๋ค. ์ด๋ฅผ http://localhost:8080/index.html ๋ก ์ ์ํ์ ๋, index.html ํ์ผ์ ์๋ตํ ์ ์๋๋ก ํ๋ค.
try (InputStream in = connection.getInputStream(); OutputStream out = connection.getOutputStream()) {
// TODO ์ฌ์ฉ์ ์์ฒญ์ ๋ํ ์ฒ๋ฆฌ๋ ์ด ๊ณณ์ ๊ตฌํํ๋ฉด ๋๋ค.
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
if (line == null) {return ;}
String[] tokens = line.split(" ");
while (!"".equals(line)) {
line = br.readLine();
}
byte[] body = Files.readAllBytes(new File("./webapp" + tokens[1]).toPath());
// byte[] body = "Hello World".getBytes();
DataOutputStream dos = new DataOutputStream(out);
response200Header(dos, body.length);
responseBody(dos, body);
} catch (IOException e) {
log.error(e.getMessage());
}
InputStream, ์ฆ ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ด๋ ์์ฒญ์ ํ ์ค ๋จ์๋ก ์ฝ๊ธฐ ์ํดBufferedReader๋ฅผ ์ฌ์ฉํ๋ค.null์ธ ๊ฒฝ์ฐ๋returnline์ " " ๋จ์๋ก ์๋ผ ์ ์ฅํ๊ณ ,GET /index.html HTTP/1.1๊ฐ์ ํ์์ด๋ฏ๋กtoken[1]์ ์์ฒญํ๋ ํ์ผ์ ์ ๋ณด๊ฐ ๋ด๊ธด๋ค.- ํด๋น ํ์ผ ๋ฐ์ดํฐ๋ฅผ
byte[]๋ก ์ฝ์ดresponse์ ์ ๋ฌํ๋ค.
์๊ตฌ์ฌํญ 2 - GET ๋ฐฉ์์ผ๋ก ํ์๊ฐ์
ํ๊ธฐ
: ํ์๊ฐ์
๋ฉ๋ด๋ฅผ ํด๋ฆญํ๋ฉด http://localhost:8080/user/form.html ๋ก ์ด๋ํ๋ฉด์ ํ์๊ฐ์
์ด ๊ฐ๋ฅํ๋ค. ํ์๊ฐ์
์ ์ํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ํํ๋ก ์ฌ์ฉ์ ์
๋ ฅ๊ฐ์ด ์๋ฒ์ ์ ๋ฌ๋๋ค.
/user/create?userId=javajigi&password=password&name=JaeSung&email=javajigi%40slipp.net
HTML๊ณผ URL์ ๋น๊ตํ๊ณ ์ฌ์ฉ์ ์
๋ ฅ ๊ฐ์ ํ์ฑํด model.User ํด๋์ค์ ์ ์ฅํ๋ค.
if (url.startsWith("/user/create")) {
int index = url.indexOf("?");
if (index != -1) {
Map<String, String> map = HttpRequestUtils.parseQueryString(url.substring(index+1));
User user = new User(map.get("userId"), map.get("password"), map.get("name"), map.get("email"));
System.out.println(user);
}
}
- URL์ด
/user/create๋ก ์์ํ๋ ๊ฒฝ์ฐ, ํ์๊ฐ์ ์์ฒญ์ด๋ค. - ์ฟผ๋ฆฌ ๋ฌธ์์ด์ (์ด๋ฆ, ๊ฐ) ๊ณผ ๊ฐ์ ํํ๋ก ๋ถ์ํ๋
parseQueryString์ ์ด์ฉํ์ฌ ํ๋ผ๋ฏธํฐ๋ก๋ถํฐ ์ฌ์ฉ์ ์ ๋ ฅ ๊ฐ์ ํ์ฑํ๋ค. - ํ์ฑํ ๊ฐ์
User๊ฐ์ฒด์ ๋ด๋๋ค. - ๊ทธ ๋ค์์ ์ด๋ป๊ฒ ...
์๊ตฌ์ฌํญ 3 - POST ๋ฐฉ์์ผ๋ก ํ์๊ฐ์
ํ๊ธฐ
: http://localhost:8080/user/form.html ํ์ผ์ form ํ๊ทธ ๋ฉ์๋๋ฅผ POST ๋ก ์์ ํ ํ ํ์๊ฐ์
์ ๊ตฌํํ๋ค.
else if (tokens[0].equals("POST")) {
while(!line.equals("")) {
line = br.readLine();
if (line.contains("Content-Length")) {
String[] content = line.split(":");
contLength = Integer.parseInt(content[1].trim());
}
}
String body = IOUtils.readData(br, contLength);
Map<String, String> map = HttpRequestUtils.parseQueryString(body);
User user = new User(map.get("userId"), map.get("password"), map.get("name"), map.get("email"));
System.out.println(user);
}
/user/create๋ถ๊ธฐ์์ ๊ฑธ๋ฌ์ง๊ณ ,GETorPOST์ด๋ฏ๋ก ํ ๋ฒ ๋ ๋ถ๊ธฐ๋ฅผ ๋๋๋ค.-
HTTP header์ body ์ฌ์ด์ ๋น ๊ณต๋ฐฑ์ด ์กด์ฌํ๋ค๊ณ ํ์ผ๋ฏ๋ก, ๊ทธ ์ ๊น์ง HTTP ์์ฒญ์ ์ฝ์ผ๋ฉด์,
Content-Length๊ฐ์ ํ์ํ๋ค.- ์ด๋,
Content-Length: 55์ ๊ฐ์ ํ์์ด๋ผcontent[1]์ ์ฒซ๋ฒ์งธ ์๋ฆฌ์ ๊ณต๋ฐฑ์ด ํฌํจ๋๋ฏ๋กtrim()์ ์ด์ฉํด ์ด๋ฅผ ์ ๊ฑฐํ๋ค.
- ์ด๋,
IOUtils.readData์reader์size๋ฅผ ์ฃผ๊ณ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ณGET๊ณผ ๋์ผํ๊ฒ ์ฌ์ฉ์ ์ ๋ ฅ ๊ฐ์ ํ์ฑํ๋ค.
์๊ตฌ์ฌํญ 4 - 302 status code ์ ์ฉ
: ํ์๊ฐ์
์ ์๋ฃํ ํ /index.html ๋ก ์ด๋ํ๊ณ ์ถ๋ค.
response302Header(dos, "/index.html");
...
private void response302Header(DataOutputStream dos, String url) {
try {
dos.writeBytes("HTTP/1.1 302 Redirect \r\n");
dos.writeBytes("Location: " + url + " \r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
}
}
-
302 code๋ 3XX Redirection ํด๋์ค์ ์ํ๋ HTTP ์ํ์ฝ๋์ด๋ค.
์๊ตฌ์ฌํญ 5 - ๋ก๊ทธ์ธํ๊ธฐ
: ๋ก๊ทธ์ธ ๋ฉ๋ด๋ฅผ ํด๋ฆญํ๋ฉด ~/user/login.html ๋ก ์ด๋ํด ๋ก๊ทธ์ธํ ์ ์๋ค. ์ฑ๊ณตํ๋ฉด /index.html ๋ก ์ด๋ํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด /user/login_failed.html ๋ก ์ด๋ํด์ผ ํ๋ค.
์์์ ํ์๊ฐ์
ํ ์ฌ์ฉ์๋ก ๋ก๊ทธ์ธํ ์ ์์ด์ผ ํ๋ค. ๋ก๊ทธ์ธ ์ฑ๊ณต ์, ์ฟ ํค๋ฅผ ํ์ฉํด ๋ก๊ทธ์ธ ์ํ๋ฅผ ์ ์งํ ์ ์์ด์ผ ํ๋ค. ๋ก๊ทธ์ธ์ด ์ฑ๊ณตํ ๊ฒฝ์ฐ ์์ฒญ ํค๋์ Cookie ํค๋ ๊ฐ์ด logined=true, ์คํจํ ๊ฒฝ์ฐ logined=false ๋ก ์ ๋ฌ๋์ด์ผ ํ๋ค.
else if (url.equals("/user/login")) {
if (tokens[0].equals("POST")) {
while(!line.equals("")) {
line = br.readLine();
log.debug("Header : {}", line);
if (line.contains("Content-Length")) {
String[] content = line.split(":");
contLength= Integer.parseInt(content[1].trim());
}
}
String body = IOUtils.readData(br, contLength);
Map<String, String> map = HttpRequestUtils.parseQueryString(body);
User user = DataBase.findUserById(map.get("userId"));
log.debug("User : {}", user);
if (user == null) {
responseFail(dos, "/user/login_failed.html");
return;
}
if (user.getPassword().equals(map.get("password"))) {
response302Logined(dos);
} else {
responseFail(dos, "/user/login_failed.html");
return;
}
}
}
private void response302Logined(DataOutputStream dos) {
try {
dos.writeBytes("HTTP/1.1 302 Redirect \r\n");
dos.writeBytes("Location: /index.html \r\n");
dos.writeBytes("Set-Cookie: logined=true \r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
}
}
private void responseFail(DataOutputStream dos, String url) throws IOException {
byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath());
response200Header(dos, body.length);
responseBody(dos, body);
}
/user/login์ POST๋ฅผ ๋ณด๋ด๋ ๊ฒฝ์ฐ, HTTP body๋ฅผ ์ฝ์ด ์ฌ์ฉ์ ์ ๋ ฅ ๊ฐ์ ์ถ์ถํ๋ค.- DB์ ์ ์ฅ๋์ด ์๋ ๊ฐ๊ณผ ๋น๊ตํ ํ, ๊ฐ ๊ฒฝ์ฐ์ ๋ง๊ฒ response๋ฅผ ์ ๋ฌํ๋ค.
์๊ตฌ์ฌํญ 6 - ์ฌ์ฉ์ ๋ชฉ๋ก ์ถ๋ ฅ
: ์ฌ์ฉ์๊ฐ ๋ก๊ทธ์ธ ์ํ์ธ ๊ฒฝ์ฐ http://localhost:8080/user/list ๋ก ์ ๊ทผํ์ ๋ ์ฌ์ฉ์ ๋ชฉ๋ก์ ์ถ๋ ฅํ๋ค. ๋ก๊ทธ์ธํ์ง ์์ ์ํ๋ผ๋ฉด login.html ๋ก ์ด๋ํ๋ค.
else if (url.equals("/user/list")) {
if (tokens[0].equals("GET")) {
while(!line.equals("")) {
line = br.readLine();
log.debug("Header : {}", line);
if (line.contains("Cookie")) {
login = isLogin(line);
}
}
if (!login) {
responseFail(dos, "/user/login.html");
return;
}
Collection<User> users = DataBase.findAll();
StringBuilder sb = new StringBuilder();
sb.append("<h1>ํ์ ๋ชฉ๋ก</h1>");
for (User user : users) {
sb.append("<p>" + user.getUserId() + "</p>");
sb.append("<p>" + user.getName() + "</p>");
sb.append("<p>" + user.getEmail() + "</p>");
}
sb.append("<h2>๋ชฉ๋ก ๋</h2>");
byte[] body = sb.toString().getBytes();
response200Header(dos, body.length);
responseBody(dos, body);
}
}
-
Cookie๊ฐ์ ๊ฐ์ ธ์ ๋ก๊ทธ์ธํ ์ฌ์ฉ์์ธ์ง ํ๋จํ๋ค.- ๋ก๊ทธ์ธํ์ง ์์ ๊ฒฝ์ฐ
login.html์ ์ ๋ฌํ๋ค. Database.findAll()๋ก ํ์๊ฐ์ ๋ ๋ชจ๋ ์ฌ์ฉ์๋ฅผ ๊ฐ์ ธ์จ๋ค.StringBuilder๋ฅผ ์ด์ฉํด ์ถ๋ ฅํ HTML code๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ responseํ๋ค.
- ๋ก๊ทธ์ธํ์ง ์์ ๊ฒฝ์ฐ
String์ ๋ถ๋ณ ๊ฐ์ฒด์ด๋ค. 2๊ฐ์ String ๊ฐ์ฒด๊ฐ ์์ ๋, ๋ ๊ฐ์ฒด๋ฅผ ๋ํ๋ ์ฐ์ฐ์ ์๋ก์ด String์ ์์ฑํ๋ค. ์ด ๋, ๋ฉ๋ชจ๋ฆฌ ํ ๋น๊ณผ ํด์ ๋ฅผ ๋ฐ์์์ผ ๋ํ๋ ์ฐ์ฐ์ด ๋ง์์ง์๋ก ์ฑ๋ฅ์ ์ผ๋ก ์ข์ง ์๋ค. ๋ฐ๋ผ์, ๋ฌธ์์ด์ ๋ํ ๋ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋ ๊ธฐ์กด์ ๋ฐ์ดํฐ์ ๋ํ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋
StringBuilderํด๋์ค๊ฐ ์๊ฒจ๋ฌ๋ค.
์๊ตฌ์ฌํญ 7 - CSS ์ง์ํ๊ธฐ
: CSS ํ์ผ์ ์ง์ํ๋๋ก ๊ตฌํํ๋ค.
else if (url.endsWith(".css")) {
byte[] body = Files.readAllBytes(new File("./webapp" + url).toPath());
response200CssHeader(dos, body.length);
responseBody(dos, body);
}
...
private void response200CssHeader(DataOutputStream dos, int length) {
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes("Content-Type: text/css\r\n");
dos.writeBytes("Content-Length: " + length + "\r\n");
dos.writeBytes("\r\n");
} catch (IOException e) {
log.error(e.getMessage());
}
}
- HTTP ์์ฒญ์ header์
./css/style.css์ ๊ฐ์ด ๋ ๋ผ์ค๊ธฐ์,endsWith()๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์ด๋ฅผ ์ถ์ถํ๋ค. Content-Type์ดtext/css๋ก ๋ฌ๋ผ์ง ๊ฒ์ ํ์ธํ ์ ์๋ค.
๐ฉ ์ถ๊ฐ ํ์ต ์๋ฃ
๐ง Git๊ณผ Github
Git
: ์ต๊ทผ์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ ๋ฒ์ ๊ด๋ฆฌ ์์คํ
(Version Control System) ์ ํ ์ข
๋ฅ, VCS์ ๋ํ ๊ธฐ๋ณธ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋๊ตฌ์ด๋ค.
Github
: Git์ด ์ ๊ณตํ๋ ๊ธฐ๋ฅ๊ณผ ๋๋ถ์ด ๊ฐ๋ฐ์๋ค์ด ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์น ์ ํ๋ฆฌ์ผ์ด์
์ด๋ค.
์ฐธ๊ณ ๋ธ๋์น rebase ๋ฑ์ ๋ฐฐ์ฐ๋ ์ค์ต GUI ๋๊ตฌ
๐ง ๋น๋ ๋๊ตฌ ๋ฉ์ด๋ธ
๋น๋ ๋๊ตฌ
: ํ๋ก์ ํธ์ ๊ด๋ จํ ์ค์ ์ ๊ด๋ฆฌํ๋ฉด์ ์์ค์ฝ๋(ํ๋ก๋์
, ํ
์คํธ)์ ๋ํ ์ปดํ์ผ, ์ปดํ์ผ์ ์ํด ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ด๋ฆฌ, ํ
์คํธ, ๋ฐฐํฌ๋ฅผ ์ํ ํจํน ์์
๋ฑ์ ์์
์ ์๋ํํ ์ ์๋๋ก ์ง์ํ๋ ๋๊ตฌ
- ํ๋ก์ ํธ ๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ์ ์์กด์ฑ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ๋ฏ๋ก, ํ๋ก์ ํธ๋ฅผ ํตํฉ ๊ฐ๋ฐ ๋๊ตฌ ํ๋ก์ ํธ๋ก ๋ณํํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค.
- ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์์ ๋ฐ์ํ๋ ๋จ์, ๋ฐ๋ณต์ ์ธ ์์ ์ ์๋ํํ ์ ์๋ค.
- ๊ฐ๋จํ ๋ฐฐํฌ ์์ ๊น์ง ์ํํ ์ ์๋ค.
Maven (๋ฉ์ด๋ธ)๊ณผ Gradle (๊ทธ๋๋ค)
๋ฉ์ด๋ธ
: ์๋ฐ ์ง์์์ ๋ ์ค๋ซ ๋์ ์ฌ์ฉํด์จ ๋น๋ ๋๊ตฌ๋ก, ์ค์ ํ์ผ์ XML๋ก ์์ฑํ๋ค.
- XML์ ์ฌ์ฉํ๊ธฐ์ ํ๋ก์ ํธ์ ํฌ๊ธฐ๊ฐ ์ปค์ง์๋ก ๋ด์ฉ์ด ๊ธธ์ด์ง๊ณ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค.
๊ทธ๋๋ค
: ์ต๊ทผ ์ธ๊ธฐ๋๊ฐ ๋์์ง๋ ๋น๋ ๋๊ตฌ๋ก, ๊ทธ๋ฃจ๋น ๋ผ๋ ์ธ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ํ์ผ์ ๊ด๋ฆฌํ์ฌ ์ค์ ํ์ผ์ ๋ํ ์ ์ฐ์ฑ๋ ๋๊ณ , ์ฝ๋ฉ๋๋ ๋ฉ์ด๋ธ์ XML์ ๋นํด ์ ๋ค.
- ๋น๋ ์๋๊ฐ ๋ฉ์ด๋ธ์ ๋นํด 10~100๋ฐฐ๊ฐ๋ ๋น ๋ฅด๋ค.
- ๊ธธ์ด์ ๊ฐ๋ ์ฑ ์ธก๋ฉด์์ ํจ์จ์ ์ด๋ค.
๋ ๋น๋ ๋๊ตฌ์ ๊ธฐ๋ณธ ๊ฐ๋ ์ ๋น์ทํ ์ ์ด ๋ง์, ํ๋์ ๋น๋ ๋๊ตฌ๋ง ์ตํ๋ฉด ๋ค๋ฅธ ๋น๋ ๋๊ตฌ์ ๋ํ ํ์ต ๋น์ฉ๋ ๋ฎ๋ค.
๐ง ๋๋ฒ๊น ์ ์ํ ๋ก๊น (logging)
๊ฐ๋ฐ์๋ ๋ค์๊ณผ ๊ฐ์ ๋ชฉ์ ์ผ๋ก ์๋ง์ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ค.
- ์ ํ๋ฆฌ์ผ์ด์ ์ด ์ ์์ ์ผ๋ก ๋์ํ๋์ง ํ์ธํ๊ธฐ ์ํ ๋ชฉ์
- ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ ๋ ์์ธ์ ํ์ ํ๊ธฐ ์ํ ๋๋ฒ๊น ๋ชฉ์
์ด๋, ์น์ํ System.out.println() ์ผ๋ก ์ถ๋ ฅํ๋๋ฐ, ์ด๋ ์ ํ๋ฆฌ์ผ์ด์
์ ์ฑ๋ฅ์ ์ ํ์ํค๋ ์์ธ์ด๋ค.
- ์ด๋ฅผ ์ด์ฉํด ๋๋ฒ๊น ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ฉด ํ์ผ๋ก ๋ฉ์ธ์ง๊ฐ ์ถ๋ ฅํ๊ฒ ๋๋๋ฐ, ํ์ผ์ ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ ์์ ์ ์๋นํ ๋น์ฉ์ด ๋ฐ์ํ๋ค.
logging ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ด์ ๊ฐ์ ๋จ์ ์ ๋ณด์ํ๊ธฐ ์ํด logging ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ฑ์ฅํ๋ค. ์๋ฐ ์ง์์์ ๋ง์ด ์ฌ์ฉํ๋ ๋ก๊น
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ Logback ์ด๋ค.
์๋ฐ ์ง์์๋ ๋ง์ ๋ก๊น
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ตฌํ์ฒด๊ฐ ์กด์ฌํ๋๋ฐ, ๋ ์ข์ ๊ตฌํ์ฒด๊ฐ ๋ฑ์ฅํ ๋๋ง๋ค ์ ์ฒด ์์ค์ฝ๋์์ ๋ก๊น
๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ตฌํ ๋ถ๋ถ์ ์์ ํ๋ ์ด๋ ค์์ด ์๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด SLF4J ๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ฉํด ๋ก๊น
API์ ๋ํ ์ฐฝ๊ตฌ๋ฅผ ์ผ์ํํ๋ค.
- ์ฆ, ์๋ฐ ์์ค์ฝ๋๋
SLF4J๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด ๋๋ฒ๊น ๋ฉ์ธ์ง๋ฅผ ๋จ๊ธฐ๋ฉด ์ค์ ๋ก ๋๋ฒ๊น ๋ฉ์ธ์ง๋ฅผ ์ถ๋ ฅํ๋ ๊ตฌํ์ฒด๋Log4J,Logback์ด ๋ด๋นํ๋ ๋ฐฉ์์ด๋ค.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger log = LoggerFactory.getLogger(RequestHandler.class);
- ํด๋น ํ๋ก์ ํธ๋ ๋ก๊น
๊ตฌํ์ฒด๋ก
Logback๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค. ํ์ง๋ง ์ด๋ฅผ ์ง์ ์ฌ์ฉํ์ง ์๊ณSLF4J๋ฅผ importํ๊ณ ์๋ค. Logback๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ํ ๊ตฌํ์ฒด๋ ๋ฉ์ด๋ธ ์ค์ ํ์ผ์ธpom.xml์์ ์ค์ ํ๊ณ ์๋ค.
๋ก๊ทธ ๋ ๋ฒจ
๋ก๊น
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ฉ์ธ์ง ์ถ๋ ฅ ์ฌ๋ถ๋ฅผ ๋ก๊ทธ ๋ ๋ฒจ์ ํตํด ๊ด๋ฆฌํ๋ค.
TRACE=log.trace(),DEBUG=log.debug(),INFO=log.info(),WARN=log.warn(),ERROR=log.error()-
TRACE < DEBUG < INFO < WARN < ERROR ์์ผ๋ก ๋์์ง๋ค.
- ๋ ๋ฒจ์ด ๋์ ์๋ก ์ถ๋ ฅ๋๋ ๋ฉ์ธ์ง๋ ์ ์ด์ง๊ณ , ๋ฎ์ ์๋ก ๋ ๋ง์ ๋ก๊น ๋ ๋ฒจ์ด ์ถ๋ ฅ๋๋ค.
- ๊ฐ ๋ฉ์ธ์ง์ ๋ํ ๋ก๊ทธ ๋ ๋ฒจ์ ๋ก๊น ๋ฉ์ธ์ง๋ฅผ ๊ตฌํํ ๋ ๊ฒฐ์ ๋๋ค.
๋ก๊ทธ ๋ฉ์ธ์ง ์์ฑ
๋ก๊ทธ ๋ฉ์ธ์ง ์ถ๋ ฅ ์ ๋ค์๊ณผ ๊ฐ์ ๋ฉ์ธ์ง ๊ตฌํ ๋ฐฉ์์ด ์ผ๋ฐ์ ์ด๋ค.
log.debug("New Client Connect! Connected IP : " + connection.getInetAddress() + ", Port : " + conneciton.getPort());
- ํ์ง๋ง ์์ ๊ฐ์ ๋ฐฉ์์ ๋ก๊ทธ ๋ ๋ฒจ์ด
INFO๋WARN์ธ ๊ฒฝ์ฐ ๊ตณ์ด ํด๋น ๋ฉ์๋์ ์ธ์ ์ ๋ฌ์ ์ํด ๋ฌธ์์ด์ ๋ํ๋ ๋ถ๋ถ์ด ์คํ๋ ํ์๊ฐ ์๋ค. (์๊น ์์์StringBuilder๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์ ๋์ผ) SLF4J๋ ์ด๋ฌํ ๋จ์ ๋ณด์์ ์ํด ๋์ ์ธ ๋ฉ์ธ์ง๋ฅผ ๊ตฌํํ๋ ๋ณ๋์ ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
log.debug("New Client Connect! Connected IP : {}, Port : {}", connection.getInetAddress(), conneciton.getPort());
- ์ ๋ฐฉ์์ผ๋ก
debug()๋ฉ์๋์์ ๋ก๊ทธ ๋ ๋ฒจ์ ๋ฐ๋ผ ๋ฉ์ธ์ง๋ฅผ ๋ํ ํ์๊ฐ ์๋์ง์ ์ฌ๋ถ๋ฅผ ํ๋จํ๊ฒ ๋๋ค.
Logback ์ ๋ก๊ทธ ๋ ๋ฒจ๊ณผ ๋ฉ์ธ์ง ํ์์ ๋ํ ์ค์ ํ์ผ์ logback.xml ์ด๋ฉฐ, ์ด๋ฅผ ํตํด ์ถ๋ ฅ๋๋ ๋ก๊ทธ ๋ฉ์ธ์ง์ ํจํด์ ๋ณ๊ฒฝํ ์ ์๋ค.
Log4j. SLF4J ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ณ ํ ํ๋ฆฟ ๋ํ ์กด์ฌํด, ๋จ์ ๋ฐ๋ณต์ ์ผ๋ก ๋ฐ์ํ๋ ์ฝ๋๋ฅผ ์ถ๊ฐํ์ฌ ๊ฐ๋ฐ ์์ฐ์ฑ์ ๋์ด๋ ๋ฐ ๋์์ด ๋ ์ ์๋ค.
๐ ์ถ์ฒ
์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step : ํ๋์ฉ ๋ฒ๊ฒจ๊ฐ๋ ์ํ๊ป์ง ํ์ต๋ฒ - ๋ฐ์ฌ์ฑ